package dLogistic.domain.service;

import dLogistic.application.service.PaczkaService;
import dLogistic.domain.model.Kurier.KurierTrasa;
import dLogistic.domain.model.Paczka.TrasaPaczki;
import dLogistic.infrastructure.persistence.hibernate.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import java.util.List;

/**
 * Created by Przemek on 2014-11-12.
 */
public class KurierService {
    private static SessionFactory factory;

    public Integer SprawdzTraseKuriera(String uuid_kurier, Integer dzien, String CzasStart, String CzasEnd){

        factory = HibernateUtil.getSessionFactory();
        Session session = factory.getCurrentSession();

        Transaction transaction = session.beginTransaction();

        String sql = "select * from KurierTrasa where uuid_kurier = :uuid_kurier";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(KurierTrasa.class);
        query.setParameter("uuid_kurier", uuid_kurier);
        List results = query.list();
        if (results.isEmpty()){
            transaction.rollback();
            return 0;
        }
        else {
            KurierTrasa kurierTrasa = (KurierTrasa) results.get(0);
            if (kurierTrasa.dzien() == 0 && dzien > 0){
                transaction.rollback();
                return 1;
            }
            if (kurierTrasa.dzien() > 0 && dzien == 0){
                transaction.rollback();
                return 2;
            }
            sql = "select * from KurierTrasa where uuid_kurier = :uuid_kurier and dzien = :dzien";
            sql = sql + " and (";
            sql = sql + " (CzasStart >= :CzasStart and CzasStart <= :CzasEnd) ";
            sql = sql + " or ";
            sql = sql + " (CzasEnd >= :CzasStart and CzasEnd <= :CzasEnd) ";
            sql = sql + " or ";
            sql = sql + " (CzasStart <= :CzasStart and CzasEnd >= :CzasEnd) ";
            sql = sql + ")";
            query = session.createSQLQuery(sql);
            query.addEntity(KurierTrasa.class);
            query.setParameter("uuid_kurier", uuid_kurier);
            query.setParameter("dzien", dzien);
            query.setParameter("CzasStart", CzasStart);
            query.setParameter("CzasEnd", CzasEnd);
            results = query.list();
            if (results.isEmpty()){
                transaction.rollback();
                return 0;
            }
            else {
                transaction.rollback();
                return 3;
            }
        }

    }

    public void PaczkiDlaMagazynu(String uuid_magazyn, String uuid_kurier) {
        factory = HibernateUtil.getSessionFactory();

        Session session = factory.getCurrentSession();

        Transaction transaction = session.beginTransaction();
        String sql = "select * from TrasaPaczki where uuid_kurier = :uuid_kurier and RoadEnd = :uuid_magazyn and DataStart = \"\"";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(TrasaPaczki.class);
        query.setParameter("uuid_kurier", uuid_kurier);
        query.setParameter("uuid_magazyn", uuid_magazyn);
        List results = query.list();

        transaction.rollback();
        PaczkaService paczkaService = new PaczkaService();
        for (Integer i = 0; i < results.size(); i++) {
            TrasaPaczki trasaPaczki = (TrasaPaczki) results.get(i);
            paczkaService.przekazaniePaczki(trasaPaczki.uuid_paczka(), uuid_kurier, uuid_magazyn, 1);
        }
    }


    public boolean KurierWracaDoDomu(String uuid_kurier, Integer dzien){
        factory = HibernateUtil.getSessionFactory();

        Session session = factory.getCurrentSession();

        Transaction transaction = session.beginTransaction();

        String sql = "select * from KurierTrasa where uuid_kurier = :uuid_kurier and dzien = :dzien order by CzasStart desc limit 1";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(KurierTrasa.class);
        query.setParameter("uuid_kurier", uuid_kurier);
        query.setParameter("dzien", dzien);
        List results = query.list();

        KurierTrasa trasaS = null;
        KurierTrasa trasaE = null;
        if (!results.isEmpty()){
            trasaS = (KurierTrasa) results.get(0);
        }
        else {
            transaction.rollback();
            return true;
        }


        sql = "select * from KurierTrasa where uuid_kurier = :uuid_kurier and dzien = :dzien order by CzasEnd asc limit 1";
        query = session.createSQLQuery(sql);
        query.addEntity(KurierTrasa.class);
        query.setParameter("uuid_kurier", uuid_kurier);
        query.setParameter("dzien", dzien);
        results = query.list();
        transaction.rollback();
        if (!results.isEmpty()){
            trasaE = (KurierTrasa) results.get(0);
        }
//        JOptionPane.showMessageDialog(null, trasaS.MagazynStart());
//        JOptionPane.showMessageDialog(null, trasaE.MagazynEnd());
        if (trasaS.MagazynStart().toString().equals(trasaE.MagazynEnd().toString())) {
            return true;
        }
        else {
            return false;

        }


    }
}
